package com.example.controller;

import com.alibaba.fastjson.JSONObject;
import com.example.common.AES;
import com.example.common.Result;
import com.example.dao.PassWord;
import com.example.entity.HnyyWnPermission;
import com.example.entity.User;
import com.example.exception.CustomerException;
import com.example.service.LoginService;
import jakarta.servlet.http.HttpServletRequest;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

@CrossOrigin
@RestController
public class LoginController {
    @Autowired
    private LoginService loginService;
    @PostMapping("/login")
    public Result login(@RequestBody User user) throws Exception {
        System.out.println("12312====>"+JSONObject.toJSONString(user));
        String s = AES.decrypt(user.getPassWord());
        User userInfo = loginService.login(user.getUserName(), s);
        if (userInfo == null) {
            throw new CustomerException(401, "账号或密码错误");
        }
        return Result.success(userInfo);
    }
    @GetMapping("/getPermission")
    public Result getPermission(HttpServletRequest request) {
        String user = (String) request.getAttribute("userId");
        List<HnyyWnPermission> permission = loginService.getPermission(user);
        System.out.println("permission"+JSONObject.toJSONString(permission));
        // 构建树形结构
        List<HnyyWnPermission> tree = buildPermissionTree(permission);
        System.out.println("permission tree = " + JSONObject.toJSONString(tree));
        return Result.success(tree);
    }
    @PostMapping("//forget")
    public Result forget(@RequestBody PassWord passWord,HttpServletRequest request) throws Exception {
        String s = AES.decrypt(passWord.getPassword());
        passWord.setPassword(s);
        String s1 = AES.decrypt(passWord.getNewPassword());
        passWord.setNewPassword(s1);
        String user = (String) request.getAttribute("userId");
        Integer result = loginService.forget(passWord, user);
        if(result == 0){
            return new Result(202,"原密码错误", null);
        }
        return Result.success(result);
    }

    /**
     * 构造树形结构路由
     */
    public List<HnyyWnPermission> buildPermissionTree(List<HnyyWnPermission> list) {
        Map<String, HnyyWnPermission> map = new HashMap<>();
        List<HnyyWnPermission> rootList = new ArrayList<>();

        // 第一步：先放入 map 中
        for (HnyyWnPermission item : list) {
            map.put(item.getPermissionId(), item);
        }

        // 第二步：构建父子关系
        for (HnyyWnPermission item : list) {
            if ("0".equals(item.getParentId())) {
                // 顶级节点
                rootList.add(item);
            } else {
                HnyyWnPermission parent = map.get(item.getParentId());
                if (parent != null) {
                    parent.getChildren().add(item);
                }
            }
        }

        return rootList;
    }



}
